package com.project.XXXX.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.aldjob.controllerImpl.UserController;
import com.google.gson.Gson;
import com.project.XXXX.api.SessionContext;
import com.project.XXXX.controllerInterface.IUserController;
import com.project.XXXX.inter.*;
import com.project.XXXX.model.*;
import com.project.XXXX.tools.ProjectTools;

@Controller

public class UserControllerImpl implements IUserController {

	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	
	@RequestMapping(value = "/register.do", method = RequestMethod.POST)
	public void registerHandler(@RequestParam("PhoneNumber") String PhoneNumber,
			@RequestParam("Password") String Password, @RequestParam("validateCode") String validateCode,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
	
		Map<String, String> map = new HashMap<>();
		String json;
		String code = (String) session.getAttribute("validateCode");
		String phoneNum = (String) session.getAttribute("phoneNum");
		if (StringUtils.equals(validateCode, code)&&StringUtils.equals(phoneNum, PhoneNumber)) {
			if (tbl_sysuserMapper.selectByPrimaryKey(PhoneNumber) == null) {
				tbl_sysuser record;
				tbl_employeeinfo EErecord;
				tbl_employerinfo ERrecord;
				record = new tbl_sysuser();
				record.setMobile(PhoneNumber);
				record.setPwd(Password);
				record.setCreatetime(new Date());
				ERrecord = new tbl_employerinfo();
				EErecord = new tbl_employeeinfo();
				ERrecord.setMobile(PhoneNumber);
				EErecord.setMobile(PhoneNumber);
				ERrecord.setTel(PhoneNumber);
				EErecord.setCreatetime(new Date());
				ERrecord.setCreatetime(new Date());
				ERrecord.setPwd(Password);
				ERrecord.setPwd(Password);
				tbl_sysuserMapper.insertSelective(record);
				tbl_employerinfoMapper.insertSelective(ERrecord);
				tbl_employeeinfoMapper.insertSelective(EErecord);
				session.setAttribute("User", record);
				session.setAttribute("EEinfo", EErecord);
				session.setAttribute("ERinfo", ERrecord);
				session.setMaxInactiveInterval(900);
				map.put("state", "success");
				map.put("success_description", "register success");
			} else {
				map.put("state", "error");
				map.put("error_description", "register repeat");
			}
		} else {
			map.put("state", "error");
			map.put("error_description", "validateCode error");

		}
		json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping(value = "/getMessage.do", method = RequestMethod.POST)
	public void getMessage(@RequestParam("phoneNum") String phoneNum, HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		tbl_sysuser record;
		record = tbl_sysuserMapper.selectByPrimaryKey(phoneNum);
		String validateCode = null;
		if (record == null) {
			
				try {
					validateCode = ProjectTools.getMessage(phoneNum);
					session.setAttribute("validateCode", validateCode);
					session.setAttribute("phoneNum", phoneNum);
					ProjectTools.ResponseOut(response, "{\"registed\":\"" + false + "\"}");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
		} else {

			ProjectTools.ResponseOut(response, "{\"registed\":\"" + true + "\"}");

		}

	}

	@RequestMapping(value = "/Login.do", method = RequestMethod.POST)
	public void LoginHandler(@RequestParam("PhoneNumber") String PhoneNumber, @RequestParam("Password") String Password,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		Map<String, String> map = new HashMap<>();
		String json;
		tbl_sysuser record=null;
		record = (tbl_sysuser) tbl_sysuserMapper.selectByPrimaryKey(PhoneNumber);
		if (record == null) {
			map.put("state", "error");
			map.put("error_description", "no user");
			
		}

		else {
			if (record.getPwd().equals(Password)) {
				session.setAttribute("User", record);
				tbl_employerinfo	ERrecord = (tbl_employerinfo) tbl_employerinfoMapper.selectByPrimaryKey(PhoneNumber);
				tbl_employeeinfo	EErecord = (tbl_employeeinfo) tbl_employeeinfoMapper.selectByPrimaryKey(PhoneNumber);
				session.setAttribute("EEinfo", EErecord);
				session.setAttribute("ERinfo", ERrecord);
				session.setMaxInactiveInterval(900);
				map.put("state", "success");
				map.put("success_description", "login success");
			} else {
				map.put("state", "error");
				map.put("error_description", "password wrong");
			}
		}
		json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);

	}

}